class Solution {
public:
    void dfs(vector<vector<int>> &graph, vector<int> &unions, int x, int &block)
    {
        unions[x]=block;
        for(int v:graph[x]) if(unions[v]<0) dfs(graph, unions, v, block);
    }
    
    vector<int> processQueries(int n, vector<vector<int>>& connections, vector<vector<int>>& queries) {
        vector<vector<int>> graph(n+1);
        for(auto &e:connections) {
            graph[e[0]].push_back(e[1]);
            graph[e[1]].push_back(e[0]);
        }

        vector<int> unions(n+1, -1);
        int block=0; //连通块编号

        for(int i=1; i<=n; i++) if(unions[i]<0) dfs(graph, unions, i, block), block++;

        vector<int> offline_Ts(n+1, INT_MAX);
        for(int i=queries.size()-1; i>=0; i--) {
            auto &q=queries[i];
            if(q[0]==2) offline_Ts[q[1]]=i; //记录最早离线时间
        }

        vector<int> blockmin(block, INT_MAX);
        for(int i=1; i<=n; i++) {
            if(offline_Ts[i]==INT_MAX) { //i一直在线
                int fa=unions[i];
                blockmin[fa]=min(blockmin[fa], i);
            }
        }

        vector<int> ans;
        for(int i=queries.size()-1; i>=0; i--) {
            auto &q=queries[i];
            int u=q[1], fa=unions[u];
            if(q[0]==2) {
                if(offline_Ts[u]==i) blockmin[fa]=min(blockmin[fa], u); //变回在线
            }
            else if(i<offline_Ts[u]) //已经在线
                ans.push_back(u);
            else if(blockmin[fa]!=INT_MAX) ans.push_back(blockmin[fa]);
            else ans.push_back(-1);
        }
        ranges::reverse(ans);
        return ans;
        
    }
};